package com.qyer.search;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qyer.search.enums.FieldsMode;
import com.qyer.search.model.ExtendVType;
import com.qyer.search.model.lastminute.SKU;
import com.qyer.search.model.lastminute.VSKU;
import com.qyer.search.model.lastminute.ZSKU;
import com.qyer.search.model.middle.ExtendEntity;
import com.qyer.search.model.mutable.IndexMap;
import com.qyer.words.model.Word;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

/**
 * User: Z J Wu Date: 2015/12/30 Time: 10:03 Package: com.qyer.search
 */
public class Constants {

  private static final Logger LOGGER = LoggerFactory.getLogger(Constants.class);

  // 一次性系统常量
  public static final String PROJECT_NAME;
  public static final String PROJECT_NAMESPACE;

  public static final String USER_INPUT_SEPARATOR_REGEX = "[\\s\\pP‘’“”]+";
  public static final String SEPARATOR = "#|~";
  public static final String SEPARATOR2 = "#|~";
  public static final String SEPARATOR_PARAMETER = ",";
  public static final String INDEX_NAME = "products";
  public static final String[] INDEX_TYPES = new String[]{"products"};

  public static final int SENDER_COUNT;
  public static final int EXPIRE_TIME_REDIS = 21600;
  public static final int SEARCH_THRESHOLD = 300;
  public static final int HALF_SEARCH_THRESHOLD = 150;

  public static final String SYSTEM_ENCODE = "utf8";

  public static Gson DEFAULT_GSON;

  public static final Map<String,Integer> START_POS_MAP = new HashMap<>();
  public static final Map<Integer,String> START_POS_MAP_INVERS = new HashMap<>();
  public static final Map<Integer,String> START_POS_MAP_INVERS_NAME = new HashMap<>();

  public static final int COUNTRY_TAG_TYPE = 8;
  public static final int CITY_TAG_TYPE = 9;
  public static final int CONTINENT_TAG_TYPE = 10;

  public static final Map<String, Integer> CONTINENT_MAP = new HashMap<>();
  public static final Map<Integer, String> CONTINENT_MAP2 = new HashMap<>();
  public static final Map<String, Integer> HOT_PS_2 = new HashMap<>();
  public static final Map<String, Integer> HOT_PS_1 = new HashMap<>();
  public static final Set<Integer> TYPE1SET = new HashSet<>();
  public static final Set<Integer> TYPE2SET = new HashSet<>();

  public static final Set<String> EXTEND_SET = new HashSet<>();
  public static final int DEFAULT_LIMIT = 250;
  public static final int DEFAULT_LIMIT_1 = 100;
  public static final int hash = 88;

  public static final Map<String,Integer> ChannelMap = new HashMap<>();
  public static final Map<Integer,String> INVERSE_ChannelMap = new HashMap<>();

  public static final Map<FieldsMode,String[]> FIELDS_MODE_MAP = new HashMap<>(3);

  public static final Map<Integer, ExtendVType> Channel_EX_MAP = new HashMap<>(5);
  public static final Map<Integer, Map<Integer, List<ExtendEntity>>> Channel_EX_MAP_2 = new HashMap<>(5);
  //目的地 国家tag_type
  public static final int COUNTRY_TYPE = 8;
  public static final int CITY_TYPE = 9;
  public static final int CONTINENT_TYPE = 10;
  public static final int POI_TYPE = 3;

  public static final Set<Integer> ss = new HashSet<>();
  public static final Set<Integer> HOT_COUNTRY = new HashSet<>();
  public static final Map<String, Integer> HOT_COUNTRY_MAP = new HashMap<>();

  public static final Set<Integer> HOT_CITY = new HashSet<>();
  public static final Map<String, Integer> HOT_CITY_MAP = new HashMap<>();
  public static final Map<Integer, Integer> TAGS_PREFER = new HashMap<>();
  public static final Map<String, String> SPECIAL_PREFER = new HashMap<>();

  static {
    CompositeConfiguration c = new CompositeConfiguration();
    try {
      c.addConfiguration(new SystemConfiguration());
      c.addConfiguration(new PropertiesConfiguration("search_config.properties"));
    } catch (ConfigurationException e) {
      LOGGER.error("Cannot find search_config.properties", e);
    }
    // 从系统环境变量获取基本配置
    String projectName = c.getString("system.env.search.project");
    String namespace = c.getString("system.env.search.namespace");
    PROJECT_NAME = projectName;
    PROJECT_NAMESPACE = namespace;
    LOGGER.info("[SystemEnvVar] - Project name - {}", PROJECT_NAME);
    LOGGER.info("[SystemEnvVar] - Project namespace - {}", PROJECT_NAMESPACE);
    SENDER_COUNT = (c == null) ? 10 : c.getInt("search.container.senderCount", 10);

    GsonBuilder builder = new GsonBuilder();
    DEFAULT_GSON = builder.create();

    HOT_COUNTRY.add(14);
    HOT_COUNTRY.add(215);
    HOT_COUNTRY.add(236);
    HOT_COUNTRY.add(233);

    HOT_CITY.add(11186);
    HOT_CITY.add(63);
    HOT_CITY.add(8845);

    HOT_COUNTRY_MAP.put("日本", 14);
    HOT_COUNTRY_MAP.put("泰国", 215);
    HOT_COUNTRY_MAP.put("美国", 236);
    HOT_COUNTRY_MAP.put("韩国", 233);

    HOT_CITY_MAP.put("台湾", 11186);
    HOT_CITY_MAP.put("东京", 63);
    HOT_CITY_MAP.put("普吉岛", 8845);

    EXTEND_SET.add("wifi");
    EXTEND_SET.add("签证");
    EXTEND_SET.add("入台证");

    START_POS_MAP.put("bjtj", 1);
    START_POS_MAP.put("shhz", 2);
    START_POS_MAP.put("gzsz", 3);
    START_POS_MAP.put("cdcq", 4);

    START_POS_MAP.put("inland", 5);
    START_POS_MAP.put("hmt", 6);
    START_POS_MAP.put("abroad", 7);

    START_POS_MAP_INVERS.put(1, "bjtj");
    START_POS_MAP_INVERS.put(2, "shhz");
    START_POS_MAP_INVERS.put(3, "gzsz");
    START_POS_MAP_INVERS.put(4, "cdcq");
    START_POS_MAP_INVERS.put(5, "inland");
    START_POS_MAP_INVERS.put(6, "hmt");
    START_POS_MAP_INVERS.put(7, "abroad");

    START_POS_MAP_INVERS_NAME.put(1, "北京/天津");
    START_POS_MAP_INVERS_NAME.put(2, "上海/杭州");
    START_POS_MAP_INVERS_NAME.put(3, "广州/深圳");
    START_POS_MAP_INVERS_NAME.put(4, "成都/重庆");
    START_POS_MAP_INVERS_NAME.put(5, "国内其他");
    START_POS_MAP_INVERS_NAME.put(6, "港澳台");
    START_POS_MAP_INVERS_NAME.put(7, "海外");

    CONTINENT_MAP.put("亚洲", 10);
    CONTINENT_MAP.put("欧洲", 12);
    CONTINENT_MAP.put("非洲", 76);
    CONTINENT_MAP.put("北美", 234);
    CONTINENT_MAP.put("南美", 235);
    CONTINENT_MAP.put("大洋洲", 239);
    CONTINENT_MAP.put("南极洲", 759);

    CONTINENT_MAP2.put(10, "亚洲");
    CONTINENT_MAP2.put(12, "欧洲");
    CONTINENT_MAP2.put(76, "非洲");
    CONTINENT_MAP2.put(234, "北美");
    CONTINENT_MAP2.put(235, "南美");
    CONTINENT_MAP2.put(239, "大洋洲");
    CONTINENT_MAP2.put(759, "南极洲");

    HOT_PS_2.put("wifi", 2375);
    HOT_PS_2.put("接送机", 2376);
    HOT_PS_2.put("拼车", 2376);
    HOT_PS_2.put("包车", 2376);
    HOT_PS_2.put("按摩spa",2378);
    HOT_PS_2.put("出海",2369);
    HOT_PS_2.put("海岛",2369);
    HOT_PS_2.put("美食",2380);

    HOT_PS_1.put("签证", 1785);
    HOT_PS_1.put("保险", 1049);
    HOT_PS_1.put("邮轮", 1020);
    HOT_PS_1.put("酒店", 1017);
    HOT_PS_1.put("机票", 1016);
    HOT_PS_1.put("自由行", 1018);
    HOT_PS_1.put("租车", 1021);
    HOT_PS_1.put("入台证", 1785);

    TYPE1SET.add(1016);
    TYPE1SET.add(1017);
    TYPE1SET.add(1018);
    TYPE1SET.add(1020);
    TYPE1SET.add(2410);
    TYPE1SET.add(1049);
    TYPE1SET.add(1785);
    TYPE1SET.add(1021);

    TYPE2SET.add(2375);
    TYPE2SET.add(2376);
    TYPE2SET.add(2377);
    TYPE2SET.add(2378);
    TYPE2SET.add(2379);
    TYPE2SET.add(2380);
    TYPE2SET.add(2381);
    TYPE2SET.add(2382);
    TYPE2SET.add(2369);
    TYPE2SET.add(2371);
    TYPE2SET.add(2372);
    TYPE2SET.add(2373);
    TYPE2SET.add(2831);
    TYPE2SET.add(2830);

    ChannelMap.put("web", 1);
    INVERSE_ChannelMap.put(1, "web");
    ChannelMap.put("h5", 2);
    INVERSE_ChannelMap.put(2, "h5");
    ChannelMap.put("mobile", 3);
    INVERSE_ChannelMap.put(3, "mobile");
    ChannelMap.put("app", 4);
    INVERSE_ChannelMap.put(4, "app");

    FIELDS_MODE_MAP.put(FieldsMode.RICH, ZSKU.RETAIN_RICH);
    FIELDS_MODE_MAP.put(FieldsMode.LIGHT, ZSKU.RETAIN_LIGHT);
    FIELDS_MODE_MAP.put(FieldsMode.LESSLIGHT, ZSKU.RETAIN_LIGHT_LESS);
    FIELDS_MODE_MAP.put(FieldsMode.SIMPLE, ZSKU.RETAIN_SIMPLE);

    Set<Integer> set = new HashSet<>();
    Set<Integer> set1 = new HashSet<>();
    set.add(149);
    set.add(163);
    set1.add(149);
    set1.add(125);
    set1.add(150);
    set1.add(163);

    ExtendVType extendVType = new ExtendVType(set, set1);
    Channel_EX_MAP.put(1, extendVType);

    Set<Integer> set2 = new HashSet<>();
    Set<Integer> set3 = new HashSet<>();
    set2.add(149);
    set2.add(163);
    set2.add(150);
    set3.add(149);
    set3.add(125);
    set3.add(150);
    set3.add(126);
    set3.add(127);

    ExtendVType extendVType1 = new ExtendVType(set2, set3);
    Channel_EX_MAP.put(4, extendVType1);

    Map<Integer, List<ExtendEntity>> map1 = new HashMap<>();
    List<ExtendEntity> extendEntities1 = new ArrayList<>();
    ExtendEntity extendEntity11 = new ExtendEntity(158, 150);
    extendEntities1.add(extendEntity11);
    map1.put(158, extendEntities1);

    List<ExtendEntity> extendEntities2 = new ArrayList<>();
    ExtendEntity extendEntity21 = new ExtendEntity(159, 163);
    extendEntities1.add(extendEntity21);
    map1.put(159, extendEntities1);

    Channel_EX_MAP_2.put(1, map1);

    ss.add(79430);
    ss.add(59193);
    ss.add(60720);
    ss.add(77829);
    ss.add(80605);

    TAGS_PREFER.put(1, 159);
    TAGS_PREFER.put(4, 173);
    SPECIAL_PREFER.put("美签", "签证");
  }

  public static void main(String[] args) {
    String s = "德国,;美国。zhongguo   riben";
    for (String ss : s.split(USER_INPUT_SEPARATOR_REGEX)) {
      System.out.println(String.format("[%s]", ss));
    }
  }
}
